<?php

  class Console {
  
    const EXIT_COMMAND = 'Exit';
    
    static function generate_scaffold ($model = false, $controller = false, $actions = false) {
      if (!$model) {
        echo "Model must! be specified\n";
        return false;
      }
      if (!$controller) {
        $controller = Inflector::Tableize($model);
      }
      global $Nails, $overwrite;
      $overwrite = false;
      if ($actions == '') {
        $actions = array('index', 'show', 'add', 'create', 'edit', 'update', 'delete');
      } else {
        $actions = explode(',', $actions);
      }
      ScaffoldedController::setModel($model);
      
      
      ConsoleHelper::CreateFolder('view');
      
      $controller_path_parts = preg_split('/\//', $controller);
      
      # Views
      $controller_path = '';
      foreach ($controller_path_parts as $key => $controller_path_part) {
        $controller_path .= S . $controller_path_part;
        ConsoleHelper::CreateFolder('view' . $controller_path); 
      }
      
      $temp  = "<?php\n\n";
      $temp .= "\tclass " . $controller_path_parts[count($controller_path_parts) -1] . "_controller extends AppController {\n\t\n";
      foreach ($actions as $action) {
        $temp .= "\t\tfunction " . $action . "() {\n";
        eval('$temp .= ScaffoldedController::' . $action . '(true);');
        $temp .= "\t\t}\n\t\t\n";
        $temp_view = $Nails->GetScaffoldedView($action, false);
        ConsoleHelper::CreateFile ('view' . $controller_path . S . $action . '.tpl', $temp_view);
      }
      ConsoleHelper::CreateFolder('view' . S . 'layouts');
      
      
      # Layouts
      $controller_path = '';
      foreach ($controller_path_parts as $key => $controller_path_part) {
        $controller_path .= S . $controller_path_part;
        if ($key < count($controller_path_parts) - 1) {
          ConsoleHelper::CreateFolder('view' . S . 'layouts' . $controller_path);
        } else {
          $controller = $controller_path_part;
        }
      }
      $temp_view = $Nails->GetScaffoldedView('layout', false);
      ConsoleHelper::CreateFile ('view' . S . 'layouts' . $controller_path . '.tpl', $temp_view);
      
      
      $temp .= "\n\t}\n";
      $temp .= "\n?>";
      
      
      # Contoller
      ConsoleHelper::CreateFolder('controller');
      $controller_path = '';
      foreach ($controller_path_parts as $key => $controller_path_part) {
        $controller_path .= S . $controller_path_part;
        if ($key < count($controller_path_parts) - 1) {
          ConsoleHelper::CreateFolder('controller' . $controller_path);
        }
      }
      ConsoleHelper::CreateFile('controller' . $controller_path . '_controller.php', $temp);
      
      
      $controller_path = '';
      foreach ($controller_path_parts as $key => $controller_path_part) {
        $controller_path .= S . $controller_path_part;
        if ($key < count($controller_path_parts) - 1) {
          ConsoleHelper::CreateFolder(ROOT_DIR . 'tmp' . S . 'compiled_php' . S . 'controller' . $controller_path, false);
        }
      }
      
      echo "\n";
    }
    
    static function generate_model ($model = false) {
      
      if (!$model) {
        echo "Model name must! be specified.\n";
        return false;
      }
      
      ConsoleHelper::CreateFolder('model');
      
      $temp = $GLOBALS['Nails']->view->template_dir;
      $GLOBALS['Nails']->view->template_dir = ENGINE_DIR .  'view' . S . 'console_templates' . S;
      $template = $GLOBALS['Nails']->view->assign('model_name', $model);
      $template = $GLOBALS['Nails']->view->fetch('model.tpl');
      $GLOBALS['Nails']->view->template_dir = $temp;
      
      
      ConsoleHelper::CreateFile('model' . S . $model . '.php', $template);
      
    }
  
    static function listen() {
      $console_methods = get_class_methods(__CLASS__);
      do {
        echo "phpnext\> ";
        $line = trim(fgets(STDIN));
        
        $line = preg_replace('/,[ ]*/', ',', $line);
        $args = preg_split('/[ ]+/', $line);
          $method = Inflector::Underscore($args[0] . '_' . $args[1]);
          array_shift($args);
          array_shift($args);
        
        if (in_array($method, $console_methods)) {
          
          $code = 'self::' . $method. '(\'' . implode('\', \'', $args) . '\');';
          eval($code);
          
        } elseif ($method != self::EXIT_COMMAND)  {
          echo "\n  Incorrect command or syntax error.\n\n";
        }
      } while ($method != self::EXIT_COMMAND);
    }
  }
  
?>